'''
Created on 2009-11-20

@author: yuchen

Basic Elements Test, which including:
 testBoard 
 	testBoardReset
 testPiece
 	testAddPiece
 	testEaten
 testPlayer
 testState
 	testSuccessorStateMoves
'''
import unittest, copy
import gamemodel.board
import gamemodel.player
import gamemodel.state
import gamemodel.piece
import interface.ConsoleAgent
import ai.computer
import ai.human
import game_model_test


class Test(unittest.TestCase):
	__board_for_test = None
	__player0 = None
	__player1 = None

	def testBoard(self):
		player0 = gamemodel.player.CheckerPlayer('anch')
		player1 = gamemodel.player.CheckerPlayer('yu21')
		board = gamemodel.board.CheckerBoard()
		board.setPlayer0(player0)
		board.setPlayer1(player1)
		board.reset()
		self.assertTrue(board.isEmpty(0, 1, player0))
	
	def testBoardReset(self):
		board = game_model_test.Test.getBoard()
		player0 = board.getPlayer0()
		player1 = board.getPlayer1()
		for i in range(10):
			for j in range(4):
				pos_player0 = board.getBoardPosition(i, j, player0)
				pos_player1 = board.getBoardPosition(i, j, player1)
				# Interval girds have or do not have piece
				if i % 2 == j % 2:
					self.assertNotEqual(pos_player0, None)
					self.assertNotEqual(pos_player1, None)
				else:
					self.assertEqual(pos_player0, None)
					self.assertEqual(pos_player1, None)
	
	def testPiece(self):
		piece = gamemodel.piece.CheckerPiece(game_model_test.Test.getPlayer0(), 0, 0)
		self.assertFalse(piece.king)
		
		#Test Turn king
		piece.turn_king()
		self.assertTrue(piece.king)
		pass
	
	def testAddPiece(self):
		state = gamemodel.state.CheckerState.constructNew(game_model_test.Test.getPlayer0(), game_model_test.Test.getPlayer1())
		board = state.getBoard()
		piece = gamemodel.piece.CheckerPiece(board.getPlayer0(), 5, 5)
		board.setBoardPosition(5, 5, board.getPlayer0(), piece)
		board.player0.add_piece(piece)
#		print
#		interface.ConsoleAgent.ConsoleDescriptor.drawState(state)
		self.assertFalse(board.isEmpty(5, 5, board.getPlayer0()))
		return

	def testEaten(self):
		state = gamemodel.state.CheckerState.constructNew(game_model_test.Test.getPlayer0(), game_model_test.Test.getPlayer1())
		board = state.getBoard()
		player = board.getPlayer0()
		board.deletePiece(0, 2, player)
#		print
#		interface.ConsoleAgent.ConsoleDescriptor.drawState(state)
		self.assertTrue(board.isEmpty(0, 2, board.getPlayer0()))
		
		for piece in player.pieces:
			self.assertFalse(piece.position == [0,2])
		return 
	
	def testPlayer(self):
		player0 = gamemodel.player.CheckerPlayer('anch')
		player1 = gamemodel.player.CheckerPlayer('yu21')
		self.assertFalse(player0 == player1)
		
		player3 = copy.copy(player0)
		self.assertTrue(player0 == player3)
		pass
	
	def testState(self):
		player0 = game_model_test.Test.getPlayer0()
		player1 = game_model_test.Test.getPlayer1()
		
		state = gamemodel.state.CheckerState.constructNew(player0, player1)
		self.assertEqual(state.getRound(), 0)
		self.assertEqual(state.getCurrentPlayer(), player0)
		
		players = state.getPlayers()
		self.assertEqual(players[1], player1)
	
	def testSuccessorStateMoves(self):
		state = gamemodel.state.CheckerState.constructNew(game_model_test.Test.getPlayer0(), game_model_test.Test.getPlayer1())
		moves = state.successorMove()
		
		state = state.take_micro_move(moves[0].list[0])
		state.switchPlayer()
		
		moves = state.successorMove()
		self.assertNotEqual(moves, None)


if __name__ == "__main__":
	#import sys;sys.argv = ['', 'Test.testName']
	unittest.main()